package com.aspire.core;

import java.beans.IntrospectionException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration.Node;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class XmlFromDB extends TestCase {
	
	private static final Logger logger = Logger.getLogger(XmlFromDB.class);
	private static String savePath = "F:\\ua.json";//生成文件保存目录
	/**
	 * Create the test case
	 * 
	 * @param testName
	 *            name of the test case
	 */
	public XmlFromDB(String testName) {
		super(testName);
	}

	/**
	 * @return the suite of tests being tested
	 */
	public static Test suite() {
		return new TestSuite(XmlFromDB.class);
	}

	/**
	 * Rigourous Test :-)
	 * 
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @throws ConfigurationException
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws InstantiationException 
	 * @throws IllegalAccessException 
	 * @throws IntrospectionException 
	 * @throws SAXException 
	 * @throws IOException 
	 */
	public void testApp() throws ConfigurationException,
			ClassNotFoundException, SQLException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, IOException, SAXException, IntrospectionException {

		//1.生成终端数据文件，上万
		CreateUAServiceImpl createUAService = new CreateUAServiceImpl();
		
		logger.info(StringUtils.center("开始生成通联UA数据文件", 80, "*"));

		List<Map<String, Object>> resultList = null;
		resultList = createUAService.getDevice();
		
		for( Map<String, Object> device:resultList )
		{
			mapToXML(device);
			//System.out.println( mapToXML(device) );
		}
		
		//2.建立终端文件下载索引文件
		createDeviceFullIndex();
	}
	


	public static String mapToXML( Map<String, Object> dataMap ) throws IOException, SAXException, IntrospectionException
	{
		// 创建根节点 list;   
        Element root = new Element("item");
        // 根节点添加到文档中；   
        Document doc = new Document(root);   

        
        Iterator<String> itekey = dataMap.keySet().iterator();
        while( itekey.hasNext() )
        {
        	String key = StringUtils.lowerCase(itekey.next());
        	Object value = dataMap.get(key);
        	
        	Element element = new Element(key);
        	element.setText(value==null?"":value.toString());
        	root.addContent(element);
        }

        String filename = dataMap.get("DEVICE_ID").toString() + ".xml";
        
        XMLOutputter XMLOut = new XMLOutputter();
        XMLOut.output(doc,new FileOutputStream("F:\\xmlua\\device\\" + filename));
        
        return XMLOut.outputString(doc);  
	}
	
	
	
	public void createDeviceFullIndex() throws IOException
	{
		// 创建根节点 list;   
        Element root = new Element("root");
        // 根节点添加到文档中；   
        Document doc = new Document(root);   

        Element element = new Element("version");
    	element.setText("3.0");
    	root.addContent(element);
    	
    	element = new Element("filetype");
    	element.setText("download");
    	root.addContent(element);
    	
    	Element itemselement = new Element("items");
    	root.addContent(itemselement);
    	
    	File f = new File("F:\\xmlua\\device");
    	for( File subFile:f.listFiles() )
    	{
//    		<url>http://cfg.api.i139.cn/common/uaV3/ua.json</url>
//			<filename>ua.json</filename>
//			<description>UA配置文件信息</description>
//			<updatelimit>2</updatelimit>
//			<crc32>3333305505</crc32>
    		
    		element = new Element("item");
    		element.addContent((new Element("url")).addContent("device/" + subFile.getName()));
    		element.addContent((new Element("filename")).addContent(subFile.getName()));
    		element.addContent((new Element("updatelimit")).addContent("1"));
    		element.addContent((new Element("crc32")).addContent( Long.toString(FileUtils.checksumCRC32(subFile))) );
    		itemselement.addContent(element);
    		//System.out.println( subFile.toString() );
    	}
    	

        XMLOutputter XMLOut = new XMLOutputter();
        XMLOut.output(doc,new FileOutputStream("F:\\xmlua\\FullIndex.xml"));
	}
}